<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>IP白名单管理</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            font-weight: 300;
        }

        .content {
            padding: 40px;
        }

        .form-section {
            background: #f8f9fa;
            padding: 30px;
            border-radius: 15px;
            margin-bottom: 30px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 15px;
            border: 2px solid #e1e5e9;
            border-radius: 10px;
            font-size: 16px;
        }

        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 10px;
            font-size: 16px;
            cursor: pointer;
            width: 100%;
        }

        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 10px;
            display: none;
        }

        .result.success {
            background: #d4edda;
            color: #155724;
        }

        .result.error {
            background: #f8d7da;
            color: #721c24;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>IP白名单管理</h1>
            <p>安全地管理您的IP访问控制列表</p>
        </div>

        <div class="content">
            <div class="form-section">
                <h2>添加IP地址</h2>
                <form id="addIpForm">
                    <div class="form-group">
                        <label for="ipAddress">IP地址</label>
                        <input 
                            type="text" 
                            id="ipAddress" 
                            name="ip" 
                            placeholder="例如: 192.168.1.1"
                            required
                        >
                    </div>
                    <button type="submit" class="btn" id="submitBtn">
                        添加到白名单
                    </button>
                </form>
                <div id="result" class="result"></div>
            </div>
        </div>
    </div>

    <script>
        function showResult(message, isSuccess = true) {
            const resultDiv = document.getElementById('result');
            resultDiv.textContent = message;
            resultDiv.className = `result ${isSuccess ? 'success' : 'error'}`;
            resultDiv.style.display = 'block';
            
            setTimeout(() => {
                resultDiv.style.display = 'none';
            }, 5000);
        }

        async function addIpToWhitelist(ip) {
            try {
                const response = await fetch('/api/whitelist/add', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ ip })
                });

                const data = await response.json();

                if (response.ok) {
                    showResult(`✅ ${data.message} (总数: ${data.data.totalCount})`, true);
                    document.getElementById('ipAddress').value = '';
                } else {
                    showResult(`❌ ${data.error}`, false);
                }
            } catch (error) {
                showResult('❌ 网络错误，请稍后重试', false);
            }
        }

        document.getElementById('addIpForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const ipInput = document.getElementById('ipAddress');
            const submitBtn = document.getElementById('submitBtn');
            const ip = ipInput.value.trim();

            if (!ip) {
                showResult('❌ 请输入IP地址', false);
                return;
            }

            submitBtn.disabled = true;
            submitBtn.textContent = '添加中...';

            try {
                await addIpToWhitelist(ip);
            } finally {
                submitBtn.disabled = false;
                submitBtn.textContent = '添加到白名单';
            }
        });
    </script>
</body>
</html> 